package com.thp.singleton;

import java.util.HashMap;
import java.util.Map;

// 类似Spring里面的写法,将类名注册,下次从里面直接获取
public class Singleton6 {

	private static Map<String, Singleton6> map = new HashMap<String, Singleton6>();
	
	
	static {
		Singleton6 single = new Singleton6();
		map.put(single.getClass().getName(), single);
	}
	
	protected Singleton6() {}
	
	// 静态工厂方法,返回此类唯一的实例
	public static Singleton6 getInstance(String name) {
		if(name == null) {
			name = Singleton6.class.getName();
		}
		if(map.get(name) == null) {
			try {
				map.put(name, (Singleton6) Class.forName(name).newInstance());
			} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return map.get(name);
		
	}
	
	
	
	
	
}
